<template>
	
	<view class="safetyStatement bg-white">
		<u-navbar
			title="意见反馈" 
			left-text="" 
			right-text="我的反馈" 
			@leftClick="onClickBack" 
			@rightClick="onClickRight"
			:placeholder="true"
			:fixed="true"
			:safeAreaInsetTop="true"
			class="nav"
		></u-navbar>
		<view class="bg-white">
			<u--input v-model="title" placeholder="请输入标题"></u--input>
			<u--textarea class="margin-top" v-model="safetyStatement" placeholder="请输入内容" clearable></u--textarea>
			<!-- <u-upload ref="uUpload" :fileList="fileList"
				@afterRead="afterRead"
				@delete="deletePic"
				:auto-upload="false" >
				<view class="">
					<text>添加照片</text>
				</view>
			</u-upload> -->
			<view class="submit">
				<u-button @click="submit" class="bg-blue">提交</u-button>
			</view>
			
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			title:'',
			safetyStatement: "",
			fileList: [],
		}
	},
	onLoad() {
		
	},
	methods: {
		async submit() {
			if(this.title === '') {
				uni.showToast({
					icon: 'none',
					title: '请输入标题'
				})
				return
			}
			if(this.safetyStatement === '') {
				uni.showToast({
					icon: 'none',
					title: '请输入内容'
				})
				return
			}
			const params = {
				title:this.title,
				content:this.safetyStatement
			}
			const res = await this.$API({url: '/api/index/retroactionlist', method: 'POST', data: params});
			uni.showToast({
				title: res.msg,
				duration: 2000,
				success: (res)=> {
					setTimeout(()=>{
						uni.switchTab({
							url:'/pages/my/Feedback'
						});
					},1500)
				}
			});
			
		},

		onClickBack () {
			uni.navigateBack()
		},
		// 筛选日期
		onClickRight(){
			uni.navigateTo({
				url:'/pages/my/feedbackList'
			});
		},
		afterRead (event) {
			
		},
		deletePic (event) {
			this[`fileList${event.name}`].splice(event.index, 1)
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this[`fileList${event.name}`].length
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].thumb)
				let item = this[`fileList${event.name}`][fileListLen]
				this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result
				}))
				fileListLen++
			}
		},
		uploadFilePromise(url) {
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: 'index.php/api/index/retroactionlist', // 仅为示例，非真实的接口地址
					filePath: url,
					name: 'file',
					formData: {
						user: 'test'
					},
					success: (res) => {
						console.log(res)
						setTimeout(() => {
							resolve(res.data.data)
						}, 1000)
					}
				});
			})
		},
	}
}
</script>

<style lang="scss" scoped>
	.safetyStatement {
		padding:50rpx;
		height: 100%;
		.submit {
			position: fixed;
			bottom: 30rpx;
			left: 0rpx;
			width: 100%;
			padding: 0 30rpx;
			.bg-blue {
				background-color: #1678ff;
				color: #ffffff;
			}
		}
	}

</style>
